import createRouteMap from './create-route-map'
import createRoute from './util/route'
// 将路由规则解析成路由表 并返回 路由匹配方法match 与 动态添加路由方法addRoutes
export default function createMatcher (routes) {
  // 调用 createRouteMap() 生成路由表
  const { pathList, pathMap } = createRouteMap(routes)
  // console.log(pathList)
  // console.log(pathMap)
  // 根据路由地址返回路由规则
  function match (path) {
    // 获取路由对象
    const record = pathMap[path]
    // 若路由对象存在
    if (record) {
      // 创建路由信息对象
      return createRoute(record, path)
    }
    // 若路由对象不存在
    return createRoute(null, path)
  }
  // console.log(match('/music/pop'))

  // 动态添加路由规则
  function addRoutes (routes) {
    createRouteMap(routes, pathList, pathMap)
  }

  return {
    match,
    addRoutes
  }
}
